<?php namespace Summer\Location\Models;

use Model;
use Winter\Storm\Database\Traits\Validation;

/**
 * Model
 */
class TownModel extends Model
{
    use Validation;
    
    /*
     * Disable timestamps by default.
     * Remove this line if timestamps are defined in the database table.
     */
    public $timestamps = false;


    /**
     * @var string The database table used by the model.
     */
    public $table = 'summer_location_town';

    /**
     * @var array Validation rules
     */
    public $rules = [
    ];

    protected static $nameList = [];

    public static function getNameList($area)
    {
        $area_id=$area->id??'';
        if (isset(self::$nameList[$area_id])) {
            return self::$nameList[$area_id];
        }
        return self::$nameList[$area_id] = self::where('area_id',$area_id)->orderBy('name', 'asc')->lists('name', 'code');
    }

    public function beforeSave()
    {
        //总共两位，前补零
        $this->city = sprintf("%02d",$this->city);
        $this->area = sprintf("%02d",$this->area);
        //总共六位，前补零
        $this->town = sprintf("%06d",$this->town);
    }
}
